package fu.capstone.hanoicityguide.presentation.fragment.transportation;

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.bus.BusBO;
import fu.capstone.hanoicityguide.business.bo.bus.BusBOImp;
import fu.capstone.hanoicityguide.business.model.Bus;
import fu.capstone.hanoicityguide.presentation.adapter.ListBusAdapter;

public class FragmentBusTab extends Fragment {

	private BusBO busBO;
	private ArrayList<Bus> arrBus;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View busTab = inflater.inflate(R.layout.fragment_list_bus, container,
				false);
		ListView listBus = (ListView) busTab.findViewById(R.id.listBus);
		busBO = new BusBOImp(container.getContext());
		
		arrBus = new ArrayList<Bus>();
		arrBus = busBO.listBus("", "");
		listBus.setAdapter(new ListBusAdapter(getActivity(),
				R.layout.adapter_list_bus, arrBus));

		arrBus = new ArrayList<Bus>();
		arrBus = busBO.listBus("", "");

		listBus.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Fragment browseBus = new DetailBusFragment(arrBus.get(position)
						.getId());
				if (browseBus != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.transportation_fragment_container, browseBus)
							.commit();
					getActivity().setTitle(arrBus.get(position).getName());
				}
			}
		});

		return busTab;

	}
}
